package com.lufei.dh.ExList;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lufei.dh.ExList.Adapter.Adapter_Main;
import com.lufei.dh.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 版权：易金卡沃 版权所有
 * <p>
 * 作者：lufeifei
 * <p>
 * 创建日期：2018/6/9
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
public class MainActivity extends AppCompatActivity {
    @BindView(R.id.id_list_recylerview)
    RecyclerView idListRecylerview;
    Adapter_Main adapterMain;
    @BindView(R.id.id_list_swiperefreshlayout)
    SwipeRefreshLayout idListSwiperefreshlayout;
    private List<bean_xx> data = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listmain);
        ButterKnife.bind(this);
        initView();
        setData();
        setAdapterMain();
    }

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        idListRecylerview.setLayoutManager(linearLayoutManager);
        adapterMain = new Adapter_Main(data, this);
        idListRecylerview.setAdapter(adapterMain);
        setIdListSwiperefreshlayout();
    }

    private void setData() {
        for (int i = 0; i < 20; i++) {
            data.add(new bean_xx("数据" + i, false));
        }
        adapterMain.notifyDataSetChanged();
    }

    private boolean isRefresh = false;//是否刷新中

    private void setAdapterMain() {
        adapterMain.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//          ImageView imageView= (ImageView) adapter.getViewByPosition(idListRecylerview,position,R.id.iv);
//                ImageView imageView = (ImageView) adapter.getViewByPosition(position, R.id.iv);
//                Glide.with(MainActivity.this).load(R.mipmap.arrow_b).into(imageView);
                List<bean_xx> list = adapter.getData();
                if (list.get(position).isD) {
                    list.set(position, new bean_xx(data.get(position).getName(), false));
                } else {
                    list.set(position, new bean_xx(data.get(position).getName(), true));
                }

                adapter.notifyItemChanged(position);

            }
        });
        adapterMain.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    adapterMain.loadMoreEnd();
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    adapterMain.loadMoreFail();
                                }
                            });

                        }
                    }
                }).start();
            }
        },idListRecylerview);
    }

    public static class bean_xx {
        private String name;
        private boolean isD;

        public bean_xx(String name, boolean isD) {
            this.name = name;
            this.isD = isD;
        }

        public String getName() {
            return name;
        }

        public boolean isD() {
            return isD;
        }
    }

    private void setIdListSwiperefreshlayout() {
        idListSwiperefreshlayout.setColorSchemeColors(Color.BLUE,
                Color.GREEN,
                Color.YELLOW,
                Color.RED);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        idListSwiperefreshlayout.setDistanceToTriggerSync(300);
        // 设定下拉圆圈的背景
        idListSwiperefreshlayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        // 设置圆圈的大小
        idListSwiperefreshlayout.setSize(SwipeRefreshLayout.LARGE);


        idListSwiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
//检查是否处于刷新状态
                if (!isRefresh) {
                    isRefresh = true;
                    //模拟加载网络数据，这里设置4秒，正好能看到4色进度条
                    new Handler().postDelayed(new Runnable() {
                        public void run() {

//                            //显示或隐藏刷新进度条
                            idListSwiperefreshlayout.setRefreshing(false);
//                            //修改adapter的数据
//                            data.add("这是新添加的数据");
//                            mAdapter.notifyDataSetChanged();
                            isRefresh = false;
                        }
                    }, 2000);
                }


            }
        });
    }
}
